/**
 * WeeShop 声明
 * ===========================================================
 * 网站： https://www.darlang.com
 * 标题： ECShop 小程序「weeshop 」- 基于 ECShop 为后台系统开发的非官方微信商城小程序
 * 链接： https://www.darlang.com/?p=709
 * 说明： 源码已开源并遵循 Apache 2.0 协议，你有权利进行任何修改，但请保留出处，请不要删除该注释。
 * ==========================================================
 * Copyright 2019 darlang
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ===========================================================
 */

// index.js
import { PNT, setNavBarTitle, request, showToast, pushPagePath, formatTime, scrollLoadList } from "../../../utils/utils";
import { GetCoupon } from '../../../utils/apis';

Page({
  /**
   * 页面的初始数据
   */
  data: {
    recommendLst: '',
    recommendInfo: [],
    wxaCodeStatus: true,
    page: 0,
    pages: {
      page: 1,
      size: 10,
      total: 10,
      done: false,
      loading: false
    },
    type: 0,
    show: false,
    flag: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    setNavBarTitle(PNT.assets.coupon);
    this.loginModal = this.selectComponent("#login-modal");
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // this.GetAccountLog();
    this.getRecommendInfo();
  },

  /**
   * 登录回调
   * @author darlang
   */
  loginCallback(cb) {
    if (cb.detail.type === 'success') {
      // this.getRecommendList();
      this.getRecommendInfo();
    }
  },

  pushPath(e) {
    
    console.log(e)
    const pathData = [
      { type: 'account', path: '/pages/member/account/index/index' },
      { type: 'apply', path: '/pages/member/account/apply/index' },
      { type: 'invoice', path: '/pages/member/invoice/index/index' },
      { type: 'hongbao', path: '/pages/member/score/history/history' },
      { type: 'bonus', path: '/pages/member/account/bonus/index' },
      { type: 'card', path: '/pages/member/score/history/history' },
      { type: 'points', path: '/pages/member/account/points/index' },
      { type: 'index', path: '/pages/index/index' },
    ];
    pushPagePath(e, pathData);
  },


  /**
   * 获取推荐记录
   * status 0等待处理 1已分成 2已取消 3已撤销
   * type 0注册分成 1订单分成
   * @author darlang
   */
  getRecommendList() {
    if (this.data.pages.done) {
      return false;
    }
    wx.showLoading({ title: '加载中...', mask: true });
    GetRecommendList(this.data.pages.page, this.data.pages.size).then(res => {
      for (let i = 0; i < res.bonues.length; i++) {
        res.bonues[i].time = formatTime(res.bonues[i].time, 'Y-M-D h:i:s');
      }
      const lst = scrollLoadList(this, res, 'bonues', 'recommendLst');
      this.setData({
        recommendLst: lst
      });
    });
  },

  /**
   * 获取推荐信息
   * @author darlang
   */
  getRecommendInfo() {
    if (!this.loginModal.check()) {
      return false;
    }
    wx.showLoading({ title: '加载中...', mask: true });
    var user = wx.getStorageSync('userInfo');
    if (!user || user == undefined) {
      return false;
    }
    var type = this.data.type;
    GetCoupon(user.user_id, type).then(res => {
      var recommendInfo = this.data.recommendInfo;
      if (res.data.length > 0) {

        this.setData({
          recommendInfo: res.data,
        })
      } else {
        wx.showLoading({
          title: '数据加载完毕',
        })
        setTimeout(function (e) {
          wx.hideLoading();
        }, 2000)
      }
    });
  },




  show: function (e) {
    this.setData({
      show: true,
    })
  },
  hide: function (e) {
    this.setData({
      show: false,
    })
  },

  key: function (e) {
    this.setData({
      key: e.detail.value
    })
  },
  pwd: function (e) {
    this.setData({
      pwd: e.detail.value
    })
  },
  gosubmit: function (e) {
    if (!this.data.flag) {
      wx.showLoading({
        title: '兑换中...',
      })
      setTimeout(function () {
        wx.hideLoading();
      }, 2000)
      return;
    }
    this.setData({
      flag: false,
    })
    var that = this;
    if (!this.data.key) {
      wx.showToast({
        title: '请输入红包口令',
      })
      this.setData({
        flag: true,
      })
      return;
    }
    if (!this.data.pwd) {
      wx.showToast({
        title: '请输入红包密令',
      })
      this.setData({
        flag: true,
      })
      return;
    }
    var user_id = wx.getStorageSync('userInfo').user_id;
    GetgetBonus(user_id, this.data.key, this.data.pwd).then(res => {
      if (res.error_code == 10000) {
        wx.showToast({
          title: res.msg,
        })
        that.getRecommendInfo();
      } else {
        wx.showToast({
          title: res.msg,
          icon: 'none'
        })
      }
      this.setData({
        flag: true,
        show: false,
      })
    })
  },

  /**
   * 切换弹窗
   * @author darlang
   */
  switchRulesPop() {
    this.setData({
      rulesPopup: !this.data.rulesPopup
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.setNavigationBarColor({
      frontColor: "#ffffff",
      backgroundColor: "#F6344D",
      animation: {
        duration: 300,
        timingFunc: "linear"
      }
    });
    // this.getWxQrcode();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  },

  load: function (e) {
    var that = this;
    var items = e.currentTarget.dataset.type;
    that.setData({
      type: items,
      recommendInfo: [],

    })
    this.getRecommendInfo();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    // this.getRecommendInfo();
    wx.showLoading({
      title: '数据加载完毕',
    })
    setTimeout(function () {
      wx.hideLoading();
    }, 2000)
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    const u = wx.getStorageSync('userInfo') || '';
    if (!u) {
      showToast('登录异常,分享失败');
      return false;
    }
    var shareName = this.data.siteInfo.name || '';
    var imgUrl = '/images/default_activity-' + parseInt(Math.random() * 2) + '.png';
    var shareLink = '/pages/index/index?inviteCode=' + (u.id || '');
    return {
      title: shareName || PNT.default,
      imageUrl: imgUrl || '/images/default_image.png',
      path: shareLink,
      success() { },
      fail() { },
      complete() { }
    };
  }
});